<template>
  <!-- 所属小区、苑/期/区、幢号、单元、室  操作 -->
  <div class="adminDefault">
    <!-- 搜索区域 -->
    <el-form ref="dataForm" class="adminDefault_nav">
      <div>
        <span style="margin-left:20px;">苑/期:</span>
        <el-input v-model="house_court" style="width:120px;" />
        <span style="margin-left:60px;">幢:</span>
        <el-input v-model="house_building" style="width:120px;" />
        <span style="margin-left:60px;">单元:</span>
        <el-input v-model="house_single" style="width:120px;" />
        <span style="margin-left:60px;">室:</span>
        <el-input v-model="house_rooms" style="width:120px;" />
        <el-button
          v-if="button1"
          class="add"
          type="primary"
          style="float:right;margin-right:20px;"
          @click="creat"
        >添加一条</el-button>
        <el-button type="primary" style="float:right;margin-right:40px" @click="Search">查询</el-button>
      </div>
    </el-form>
    <el-table :data="houseAlls" style="width: 100%" :border="true" :highlight-current-row="true">
      <el-table-column label="房屋ID" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper" align="center">
            <el-tag size="medium">{{ scope.row.house_id }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所属小区" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper" align="center">
            <!-- <span>{{ scope.row.house_name }}</span> -->
            <span>大都会风情苑</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="苑/期/区" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.house_court }}</span> 区/期
          </div>
        </template>
      </el-table-column>

      <el-table-column label="幢号" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.house_building }}幢</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="单元" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.house_single }}单元</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="室" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.house_rooms }}室</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-if="button2" size="medium" type="success" @click="updataHouse(scope.row)">编辑</el-button>
          <el-button v-if="button3" size="medium" type="danger" @click="deleteHouse(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改一条 -->
    <el-dialog :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item v-if="aaa" label="房屋ID:">
          <span>{{ temp.house_id }}</span>
        </el-form-item>
        <el-form-item label="苑/期:">
          <span v-if="fff">{{ temp.house_court }}</span>
          <el-input v-if="ddd" ref="house_courts" v-model="temp.house_court" />
        </el-form-item>
        <el-form-item label="幢:">
          <el-input ref="house_buildings" v-model="temp.house_building" />
        </el-form-item>
        <el-form-item label="单元:">
          <el-input ref="house_singles" v-model="temp.house_single" />
        </el-form-item>
        <el-form-item label="房间号:">
          <el-input ref="house_roomss" v-model="temp.house_rooms" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="bbb" type="primary" @click="updateData">更改</el-button>
        <el-button v-if="ccc" type="primary" @click="creatData">新增</el-button>
      </div>
    </el-dialog>
    <!-- 确认删除 -->
    <el-dialog :visible.sync="deleteDataFrom" width="30%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <span>确定删除这一条吗？</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDataFrom = false">取消</el-button>
        <el-button type="primary" @click="deleteData">确定删除</el-button>
      </div>
    </el-dialog>
    <!-- 分页器1 -->
    <el-col v-if="one" :span="24" class="toolbar" style="text-align: center;">
      <el-pagination
        background
        :current-page="currentPage"
        :page-count="pageNum"
        :page-size="pagesize"
        style="margin-top:30px;margin-bottom:30px"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-col>
    <!-- 分页器2 -->
    <el-col v-if="two" :span="24" class="toolbar" style="text-align: center;">
      <el-pagination
        background
        :current-page="currentPages"
        :page-count="pageNums"
        :page-size="pagesizes"
        style="margin-top:30px;margin-bottom:30px"
        layout="total, prev, pager, next, jumper"
        :total="totals"
        @size-change="handleSizeChanges"
        @current-change="handleCurrentChanges"
      />
    </el-col>
  </div>
</template>
<script>
import request from '@/utils/request'
import { Message, popconfirm } from 'element-ui'
import { getToken, getAccount, getButtonpermsStr } from '@/utils/auth'
export default {
  data() {
    return {
      houseAlls: [],
      house_courts: '',
      house_buildings: '',
      house_singles: '',
      house_roomss: '',
      currentPage: 1, // 初始页
      pagesize: 10, // 当前页面内的列表行数
      pageNum: 1, // 总数页数，
      total: 0,
      dialogFormVisible: false,
      deleteDataFrom: false,
      aaa: false, // （增加修改）表单ID显示
      bbb: true, // （增加修改）表单更新按钮显示
      ccc: true, // （增加修改）表单新增按钮显示
      ddd: true, // 权限ID输入框显示
      eee: true, // 密码框显示
      fff: true, // 权限ID
      one: true,
      two: false,
      rules: {},
      dancing: [], // 幢
      options: [],
      button1: false,
      button2: false,
      button3: false,
      buttonpermsStr: getButtonpermsStr(),
      temp: {
        house_id: '',
        house_name: '',
        house_court: '',
        house_building: '',
        house_single: '',
        house_rooms: '',
        token: getToken()
      },
      house_building: '',
      house_court: '',
      house_rooms: '',
      house_single: '',
      currentPages: 1, // 初始页
      pagesizes: 10, // 当前页面内的列表行数
      pageNums: 1, // 总数页数，
      totals: 0,
      search: {
        house_building: 0,
        house_court: 0,
        house_rooms: 0,
        house_single: 0,
        current_page: 1,
        page_size: 10
      }
    }
  },
  mounted() {
    if (this.buttonpermsStr !== undefined) {
      if (this.buttonpermsStr.indexOf(137) > -1) {
        this.button1 = true
      }
      if (this.buttonpermsStr.indexOf(138) > -1) {
        this.button2 = true
      }
      if (this.buttonpermsStr.indexOf(139) > -1) {
        this.button3 = true
      }
    }
    this.two = false
    this.one = true
    this.getHouseList()
  },
  methods: {
    // 查询
    Search() {
      if (this.house_building == '') {
        this.search.house_building = 0
      } else {
        this.search.house_building = this.house_building
      }
      if (this.house_court == '') {
        this.search.house_court = 0
      } else {
        this.search.house_court = this.house_court
      }
      if (this.house_rooms == '') {
        this.search.house_rooms = 0
      } else {
        this.search.house_rooms = this.house_rooms
      }
      if (this.house_single == '') {
        this.search.house_single = 0
      } else {
        this.search.house_single = this.house_single
      }
      if (
        this.house_single == '' &&
        this.house_court == '' &&
        this.house_rooms == '' &&
        this.house_single == '' &&
        this.house_building == ''
      ) {
        this.two = false
        this.one = true
        this.getHouseList()
      } else {
        this.two = true
        this.one = false
        this.search.current_page = this.currentPages
        return request({
          url: '/v3/admin/houseByHouse/list',
          method: 'post',
          headers: {
            account_type: getAccount()
          },
          data: this.search
        }).then(res => {
          this.houseAlls = res.houseAllLists
          this.currentPages = res.current_page
          this.pageNums = res.pages
          this.totals = res.total_record
        })
      }
    },
    // 当前页
    handleSizeChange: function(pagesize) {
      this.pagesize = pagesize
      this.getHouseList()
    },
    // 换页：更新列表数据
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.getHouseList()
    },
    // 当前页
    handleSizeChanges: function(pagesizes) {
      this.pagesizes = pagesizes
      this.Search()
    },
    // 换页：更新列表数据
    handleCurrentChanges: function(currentPages) {
      this.currentPages = currentPages
      this.Search()
    },
    // 修改
    updataHouse(row) {
      this.dialogFormVisible = true
      this.aaa = true
      this.bbb = true
      this.ccc = false
      this.ddd = true
      this.eee = false
      this.fff = false
      this.temp = Object.assign(row)
    },
    // 新增
    creat() {
      this.dialogFormVisible = true
      this.aaa = false
      this.ccc = true
      this.bbb = false
      this.ddd = true
      this.eee = true
      this.fff = false
      this.temp = {
        house_building: '',
        house_court: '',
        house_rooms: '',
        house_single: ''
      }
    },
    // 删除
    deleteHouse(row) {
      this.deleteDataFrom = true
      this.temp = {
        house_id: row.house_id
      }
    },
    // 确认修改
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          return request({
            url: '/v3/admin/house/update',
            method: 'post',
            headers: {
              account_type: getAccount()
            },
            data: this.temp
          }).then(res => {
            if (res.tip === '更新成功') {
              this.dialogFormVisible = false
              this.$router.go(0) // 刷新页面
              Message({
                message: '更新成功',
                type: 'success',
                duration: 0.5 * 1000
              })
            }
          })
        }
      })
    },
    // 确认添加
    creatData() {
      return request({
        url: '/v3/admin/house/insert',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        if (res.tip === '添加成功.') {
          this.dialogFormVisible = false
          this.$router.go(0) // 刷新页面
          Message({
            message: '添加成功',
            type: 'success',
            duration: 0.5 * 1000
          })
        }
      })
    },
    // 确认删除
    deleteData() {
      return request({
        url: '/v3/admin/house/dalete',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        this.deleteDataFrom = false
        if (res.tip === '删除成功.') {
          this.deleteDataFrom = false
          this.$router.go(0) // 刷新页面
          Message({
            message: '删除成功',
            type: 'success',
            duration: 0.5 * 1000
          })
        }
      })
    },
    // 获取所有
    getHouseList() {
      return request({
        url: '/v3/admin/house/list',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: {
          current_page: this.currentPage,
          page_size: this.pagesize,
          area_id: 0
        }
      }).then(res => {
        this.houseAlls = []
        this.houseAlls = res.houseListDtos
        this.currentPage = res.current_page
        this.pageNum = res.pages
        this.total = res.total_record
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.adminDefault {
  margin-top: 60px;
  overflow: hidden;
  .adminDefault_nav {
    margin: 10px;
  }
}
</style>

